<template>
  <div class="my-container">
    <!-- 未登录 -->
    <div class="header not-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="" />
        <span class="text">登录&nbsp;/&nbsp;注册</span>
      </div>
    </div>
    <!-- /未登录 -->

    <!-- 已登录 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image
            fit="cover"
            round
            :src="userInfo.photo"
            class="avatar"
          />
          {{ userInfo.name }}
        </div>
        <div class="right">
          <van-button type="default" class="bjzl" to="/user/profile"
            >编辑资料</van-button
          >
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <div class="jishu">{{ userInfo.art_count }}</div>
          <div class="citiao">头条</div>
        </div>
        <div class="data-item">
          <div class="jishu">{{ userInfo.follow_count }}</div>
          <div class="citiao">关注</div>
        </div>
        <div class="data-item">
          <div class="jishu">{{ userInfo.fans_count }}</div>
          <div class="citiao">粉丝</div>
        </div>
        <div class="data-item">
          <div class="jishu">{{ userInfo.like_count }}</div>
          <div class="citiao">获赞</div>
        </div>
      </div>
    </div>
    <!-- /已登录 -->

    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="toutiao toutiao-shoucang shoucang"></i>
        </template>
        <template #text>
          <span class="text">收藏</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="toutiao toutiao-lishi lishi"></i>
        </template>
        <template #text>
          <span class="text">历史</span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- /导航 -->

    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      title="退出登录"
      class="login-cell"
      v-if="user"
      @click="onLogout"
      clickable
    />
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { getUserInfo } from '@/api/user.js';
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data() {
    return {
      userInfo: {}, // 用户信息
    };
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
  created() {
    // 如果用户登录了，则请求加载用户信息数据
    if (this.user) {
      this.loadUserInfo();
    }
  },
  mounted() {},
  methods: {
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo();
        this.userInfo = data.data;
      } catch (err) {
        this.$toast('获取数据失败，请稍后重试');
      }
    },
    onLogout() {
      this.$dialog
        .confirm({
          title: '确认退出吗？',
        })
        .then(() => {
          this.$store.commit('setUser', null);
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 360px;
    background: url(~@/assets/banner.png) no-repeat;
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        flex-direction: colum;
        align-items: center;
        color: #fff;
        font-size: 30px;
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
      }
      .right {
        .bjzl {
          padding: 0;
          width: 118px;
          height: 36px;
          line-height: 36px;
          color: #747474;
          border-radius: 20px;
          font-size: 20px;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .jishu {
          font-size: 36px;
        }
        .citiao {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    margin-bottom: 9px;
    .grid-item {
      .shoucang {
        color: #f39999;
      }
      .lishi {
        color: #ff9d1d;
      }
      .text {
        font-size: 28px;
      }
      .toutiao {
        font-size: 45px;
      }
    }
  }
  .login-cell {
    color: red;
    text-align: center;
    margin-top: 9px;
  }
}
</style>
